$(init);

function init() {

    copyURL();
    pageInitDo('#share', generateQR);
    $('body').on('click', '.share-btn', shareBtnClick)
             .on('click', 'ons-list-item#share', shareBtnClick)
             .on('click', 'ons-button#guide-share', shareBtnClick)
             .on('click', 'ons-card#sub-num ons-button', function() {
                document.querySelector('ons-navigator').removePage(1);
                document.querySelector('#main-tabbar').setActiveTab(3, {
                    callback: function() {
                        document.querySelector('#ac-topbar').setActiveTab(2);
                    }
                });

             });

}



function shareBtnClick() {
    navPushPage('/mobile-views/share.html');
    getSubPeople();
}

function pageInitDo(pageId, func) {
    document.addEventListener('init', function (event) {
        var page = event.target;
        if(page.matches(pageId)) {
            func();
        }
    });
}

function generateQR() {
    var URL = 'http://m.jsz800.com/';
    $('#qrcode').qrcode(URL + 'html/signup.html?invite=' + cookie("invitecode"));
    $('#inviteLink').text(URL + 'html/signup.html?invite=' + cookie("invitecode"));
    $('#invite').text(cookie("invitecode"));
}

function copyURL() {
    var URL = `http://m.jsz800.com/html/signup.html?invite=${cookie("invitecode")}`;
    var clipboard = new ClipboardJS('#inviteLink', {text: function () { return URL; }});
    clipboard.on('success', function () {notifyToast('复制成功！');});
}

// /buyer/stat_sub_number
function getSubPeople() {
    promise('GET', '/buyer/stat_sub_number', null, function(e) {
        console.log(e[0]);
        $('ons-card#sub-num span').text(e[0]);
    });
}